#include "stdafx.h"
#include "svm.h"
#include "common.h"
#include "svm_model_onevsone.h"



SVMModelOne::~SVMModelOne() {
	free_data(train_);
	free_model();
	free_param();
}


void SVMModelOne::train() {
	if (model_ != NULL && model_.get() != nullptr)
		free_model();
	model_ = shared_ptr<svm_model>(svm_train(train_.get(), param_.get()));
}


vector<double> SVMModelOne::predict(shared_ptr<svm_problem> test) {
	vector<double> res(test->l, 0);
	for (int i = 0; i < test->l; i++) {
		res[i] = svm_predict(model_.get(), test->x[i]);
	}
	return res;
}

vector<double> SVMModelOne::predict_with_probability(shared_ptr<svm_problem> test,
	vector<vector<double>>& probability){
	int num = test->l;
	vector<double> res(test->l, 0);
	probability = vector<vector<double>>(test->l, vector<double>(model_->nr_class, 0));
	for (int i = 0; i < test->l; i++) {
		double* prob = new double[model_->nr_class];
		res[i] = svm_predict_probability(model_.get(), test->x[i], prob);
		for (int j = 0; j < model_->nr_class; j++)
			probability[i][j] = prob[j];
		delete[] prob;
	}
	return res;
}

vector<double> SVMModelOne::predict_with_dec_value(shared_ptr<svm_problem>, vector<vector<double>>&) {
	return vector<double>();
}

// TODO
vector<vector<svm_node*>> SVMModelOne::get_support_vectors() {
	return vector<vector<svm_node*>>();
}
